// mypage/personalDom/personalDom.js
var app=getApp();
var util=require("../../utils/util");
var config=require("../../api/config");
var my=require("../../api/my");

Page({
  /*页面的初始数据*/
  data: {
    uid: "",
    user_img: "",
    text1: '',
    text2: '',
    text3: '',
    info: '',
    pop_show: true,
    sex_list: ["男","女"],
    pic_value: [0],
    val: 0,
    token: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    var uid=options.id;
    //
    that.get_personal_dom(uid)
    that.setData({uid:uid,token:wx.getStorageSync('token')})
  },
  // 上传头像
  choose_image:function(e){
    var that = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['album', 'camera'],
      success: (res) => {
        // console.log(res)
        wx.showLoading({
          title: '上传中'
        })
        that.get_oss(res)
      }
    })
  },
  // 选择性别
  choose_sex:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  picker_change:function(e){
    var index = e.detail.value[0];
    this.setData({val: index})
  },
  close_pop:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  confirm_sex:function(){
    var that = this,val = that.data.val,sex_list=that.data.sex_list;
    that.setData({text2:sex_list[val],pop_show: !that.data.pop_show})
  },
  // 跳转填写
  nav_userName:function(){
    var that=this,text=that.data.text1;
    wx.navigateTo({
      url: '../userName/userName?text='+text,
    })
  },
  nav_userSign:function(){
    var that=this,text=that.data.text3;
    wx.navigateTo({
      url: '../userSign/userSign?text='+text,
    })
  },
  // 保存
  confirm_text:function(){
    var that=this,info=that.data.info,user_img=that.data.user_img,text1=that.data.text1,val=that.data.val,text3=that.data.text3,change=false;
    if(user_img!=info.avatar||text1!=info.nickName||(val+1)!=info.sex||text3!=info.signature){
      info.avatar=user_img
      info.nickName=text1
      info.sex=(val+1)
      info.signature=text3
      change=true;
    }
    var params ={
      "id": that.data.uid,
      "nickName": text1,
      "signature": text3,
      "avatar": user_img,
      "orgId": info.orgId,
      "identity": info.identity,
      "sex":  val+1
    }
    that.edit_personal_dom(params,change)
  },
  // 获取个人资料
  get_personal_dom:function(id){
    wx.showLoading({title:"加载中"})
    var that=this;
    my.get_personal_dom({"id":id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        that.setData({info:info,user_img:info.avatar,text1:info.nickName,text2:that.data.sex_list[info.sex-1],text3:info.signature,val:info.sex-1,pic_value:[info.sex-1]})
      }
    })
  },
  // 修改个人资料
  edit_personal_dom:function(params,change){
    wx.showLoading({title:"加载中"})
    var that=this,token=that.data.token;
    if(change){
      my.edit_personal_dom(params,token).then(res=>{
        if(res.data.code==200){
          util.page_show_toast('修改成功',1500)
          app.globalData.change_dom=true
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            });
          },1500)
        }
      })
    }else{
      wx.navigateBack({
        delta: 1
      });
    }
  },
  // 获取oss上传图片
  get_oss:function(file){
    var that=this,randomStr=util.randomString(13);
    var params={
      "isPublic": true,
      "isHttps": true
    }
    config.get_oss(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        wx.uploadFile({
          url: info.host,
          filePath: file.tempFilePaths[0],
          name: 'file',
          formData: {
            "key": randomStr,
            "policy": info.policy,
            "OSSAccessKeyId": info.accessKeyId,
            "signature": info.signature,
            "success_action_status": '200'
          },
          success:function(){
            wx.hideLoading()
            var user_img=info.host+'/'+randomStr;
            that.setData({user_img: user_img})
          }
        })
      }
    })
  },
})